<?php

namespace app\common\model;

use app\common\model\ServiceContent as ServiceContentModel;
use app\common\model\ServiceBind as ServiceBindModel;
use app\common\model\ServicePackage as ServicePackageModel;

class ServicePackage extends BaseModel
{
    protected $name = "service_package";

    /**
     * 获取数据列表
     */
    public function getList()
	{
		return $this->order('created_at', 'desc')->paginate($this->pageLimit);
	}

    /**
     * 添加数据
     * @param array $data
     * @return string
     */
    public function addData(array $data): string
    {
        //添加时销毁自带的主键id
        unset($data['id']);
        return $this->save($data);
    }

    /**
     * 更新数据
     * @param array $data
     * @return bool
     */
    public function saveData($id, array $data): bool
    {
        $model = self::find($id);
        return $model->save($data);
    }

    /**
     * 获取全部数据
     * @return array
     */
    public function getRows(): array
    {
        return $this->field('id,unique_no,name')->select()->toArray();
    }

    /**
     * 多个id获取数据
     */
    public function getWhereIds(array $ids)
    {
        return $this->whereIn('id', $ids)->field('id,name,summary,image')->paginate($this->pageLimit);//->select()->toArray();
    }

    /**
     * 单个id获取数据
     */
    public function getWhereId($item_id, $pack_id)
    {
        $content = $this->where('id', $pack_id)->field('id,name,contentList,cycle,summary')->find();

        $item = (new ServiceBindModel)->where('item_id', $item_id)->field('name')->find();
    
        if (isset($content['contentList']) && !empty($content['contentList']))
        {
            $conArr = $content->toArray();

            $ids = [];
            foreach ($conArr['contentList'] as $key => $val)
            {
                $ids[] = $val['id'];
            }
            $data['item']['name'] = $item['name'];
            $data['pack']['name'] = $conArr['name'];
            $data['pack']['summary'] = isset($conArr['summary'])?$conArr['summary']:'';
            $data['pack']['cycle']   = isset($conArr['cycle'])?$conArr['cycle']:'';
            $data['content'] = (new ServiceContentModel)
                    ->whereIn('id', $ids)
                    ->paginate($this->pageLimit)
                    ->each(function($item, $key) use($content){
                        $frequency = '';
                        foreach ($content['contentList'] as $k => $v)
                        {
                            if ($v['id'] == $item['id'])
                            {
                                $frequency.= $v['frequency'];
                            }
                        }
                        $item['frequency'] = $frequency;
                        return $item;
                    });
        }else{
            $data['item']['name'] = $item['name'];
            $data['pack']['name'] = $content['name'];
            $data['pack']['summary'] = $content['summary'];
            $data['pack']['cycle']   = $content['cycle'];
            $data['content'] = [];
        }
        return $data;
    }
}